﻿using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Routing;
using System.Linq;
using System.Text;

namespace AppManager.Background
{
    public abstract class BaseProvider : RouteBase
    {
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            return GetMyRouteData(httpContext);
        }

        /// <summary>
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="values">参数包括action，controller，各种url中传递的参数</param>
        /// <returns></returns>
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            if (values.ContainsKey("controller") && values["controller"] != null)
            {
                VirtualControllerName = values["controller"].ToString().ToLower();
            }
            else
            {
                VirtualControllerName = string.Empty;
            }

            if (values.ContainsKey("action") && values["action"] != null)
            {
                VirtualActionName = values["action"].ToString().ToLower();
            }
            else
            {
                VirtualActionName = string.Empty;
            }

            if (values.ContainsKey("siteid") && values["siteid"] != null)
            {
                VirtualSiteId = values["siteid"].ToString().ToLower();
            }
            else
            {
                VirtualSiteId = string.Empty;
            }

            if (values.ContainsKey("channelid") && values["channelid"] != null)
            {
                VirtualChannelId = values["channelid"].ToString().ToLower();
            }
            else
            {
                VirtualChannelId = string.Empty;
            }


            var list = values.Where(s =>
                  s.Key.ToLower() != "siteid" &&
                  s.Key.ToLower() != "channelid" &&
                  s.Key.ToLower() != "controller" &&
                  s.Key.ToLower() != "action"
                );
            StringBuilder sb = new StringBuilder();
            sb.Append("?");
            foreach (var item in list)
            {
                sb.AppendFormat("{0}={1}&", item.Key, item.Value);
            }

            VirtualOther = sb.ToString().TrimEnd('&').TrimEnd('?');

            return GetMyVirtualPath(requestContext, values);
        }

        #region 需要子类重写的方法
        public abstract RouteData GetMyRouteData(HttpContextBase httpContext);

        public abstract VirtualPathData GetMyVirtualPath(RequestContext requestContext, RouteValueDictionary values);
        #endregion

        /// <summary>
        /// 获取values中数据
        /// </summary>
        /// <param name="values"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValue(RouteValueDictionary values, string key)
        {
            object result;
            values.TryGetValue(key, out result);
            return (result ?? string.Empty).ToString();
        }
        #region 公共属性
        #region RouteData中的属性
        public string HostName => HttpContext.Current.Request.Url.Host.ToLower();

        public string QueryString => HttpContext.Current.Request.Url.Query;

        /// <summary>
        ///     ~/开头的string
        /// </summary>
        public string VirtualPath
        {
            get
            {
                return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath + HttpContext.Current.Request.PathInfo; //获取相对路径   
            }
        }

        public string ControllerName
        {
            get
            {
                var values = HttpContext.Current.Request.RequestContext.RouteData.Values;
                if (values.ContainsKey("controller"))
                {
                    return values["controller"].ToString().ToLower();
                }

                return string.Empty;
            }
        }

        public string ActionName
        {
            get
            {
                var values = HttpContext.Current.Request.RequestContext.RouteData.Values;
                if (values.ContainsKey("action"))
                {
                    return values["action"].ToString().ToLower();
                }

                return string.Empty;
            }
        }

        #region 在VirtualPath中的属性
        /// <summary>
        ///     在virtualpath方法中获取的属性（注意区分controllerName）
        /// </summary>
        public string VirtualControllerName { get; set; }

        /// <summary>
        ///     在virtualpath方法中获取的属性（注意区分actionName）
        /// </summary>
        public string VirtualActionName { get; set; }

        /// <summary>
        ///     在virtualpath方法中获取的属性（注意区分siteId）
        /// </summary>
        public string VirtualSiteId { get; set; }

        public string VirtualChannelId { get; set; }
        /// <summary>
        /// ?ab=1&c=2&d=3 这些其他参数
        /// </summary>
        public string VirtualOther { get; set; }
        #endregion
        #endregion
        #endregion
    }
}